#include <spdlog/spdlog.h>
#include <spdlog/sinks/base_sink.h>
#include <spdlog/sinks/basic_file_sink.h>
#include <spdlog/sinks/stdout_color_sinks.h>
#include <spdlog/async.h>
#include <iostream>
void test_sync_colorout()
{
    auto logger=spdlog::stdout_color_mt("color_stdout");
    spdlog::flush_every(std::chrono::seconds(1));   //这个接口只能设置为全局的
    logger->flush_on(spdlog::level::debug); //遇到debug等级及以上等级的日志立即刷新
    logger->set_level(spdlog::level::trace); //设置该日志器的输出等级，只输出大于这个等级的日志
    logger->set_pattern("[%Y-%m-%d:%H-%M-%S][%-8l]: %v");

    
    logger->trace("this logger level is {}","trace");
    logger->debug("this logger level is {}","debug");
    logger->warn("this logger level is {}","warn");
    logger->critical("this logger level is {}","critical");
    std::cout<<"log begin work"<<std::endl;
    spdlog::drop(logger->name());//删除指定的日志器

}   




void test_sync_fileout()
{   
    auto logger=spdlog::basic_logger_mt("color_stdout","/root/sample-code/learn_spdlog/log.txt");
    spdlog::flush_every(std::chrono::seconds(1));   //这个接口只能设置为全局的
    logger->flush_on(spdlog::level::debug); //遇到debug等级及以上等级的日志立即刷新
    logger->set_level(spdlog::level::trace); //设置该日志器的输出等级，只输出大于这个等级的日志
    logger->set_pattern("[%Y-%m-%d:%H-%M-%S][%-8l]: %v");

    std::cout<<"log begin work"<<std::endl;
    logger->trace("this logger level is {}","trace");
    logger->debug("this logger level is {}","debug");
    logger->warn("this logger level is {}","warn");
    logger->critical("this logger level is {}","critical");

    spdlog::drop(logger->name());//删除指定的日志器
}

void test_safty_spdlog()
{
    //spdlog可能会抛出异常，所以需要对异常进行捕获
    //1.如果取两个相同名称的日志器就会抛异常
    //2.当你需要创建一个向文件中写入日志的日志器的时候如果指定的路径无效，那么也会抛出错误。
   try
   { 
        auto logger=spdlog::basic_logger_mt("color_stdout","/root/sample-code/learn_spdlog/log.txt");
        spdlog::flush_every(std::chrono::seconds(1));   //这个接口只能设置为全局的
        logger->flush_on(spdlog::level::debug); //遇到debug等级及以上等级的日志立即刷新
        logger->set_level(spdlog::level::trace); //设置该日志器的输出等级，只输出大于等于这个等级的日志
        logger->set_pattern("[%Y-%m-%d:%H-%M-%S][%-8l]: %v");

        std::cout<<"log begin work"<<std::endl;
        logger->trace("this logger level is {}","trace");
        logger->debug("this logger level is {}","debug");
        logger->warn("this logger level is {}","warn");
        logger->critical("this logger level is {}","critical");

        spdlog::drop(logger->name());//删除指定的日志器
    }
    catch(const spdlog::spdlog_ex& ex)
    {
        std::cout<<"log operation failed! the reason is: "<<ex.what()<<std::endl;
    }
}

void test_async_spdlog()
{
    //如果想让日志异步输出,只需在创建日志器的时候指定异步类型工厂即可
    auto logger=spdlog::stdout_color_mt<spdlog::async_factory>("color_stdout");
    spdlog::flush_every(std::chrono::seconds(1));   //这个接口只能设置为全局的
    logger->flush_on(spdlog::level::debug); //遇到debug等级及以上等级的日志立即刷新
    logger->set_level(spdlog::level::debug); //设置该日志器的输出等级，只输出大于这个等级的日志
    logger->set_pattern("[%Y-%m-%d:%H-%M-%S][%-8l]: %v");

   
    logger->trace("this logger level is {}","trace");
    logger->debug("this logger level is {}","debug");
    logger->warn("this logger level is {}","warn");
    logger->critical("this logger level is {}","critical");
    std::cout<<"log begin work"<<std::endl;
    spdlog::drop(logger->name());//删除指定的日志器
}


void test_common_name_logger()
{
    try
    {
        {
            auto logger1=spdlog::stdout_color_mt("name");
            spdlog::drop(logger1->name());
        }
        {
            auto logger2=spdlog::stdout_color_mt("name");
        }
    }
    catch(const std::exception& e)
    {
        std::cerr << e.what() << '\n';
    }
}

void test_error_filepath()
{   try
    {
        auto logger1=spdlog::basic_logger_mt("name","awdadafawfawad/awdada");
    }
    catch(const std::exception& e)
    {
        std::cerr << e.what() << '\n';
    }
}

//同步博客地址   
//https://blog.csdn.net/m0_73987904/article/details/146405456?sharetype=blogdetail&sharerId=146405456&sharerefer=PC&sharesource=m0_73987904&spm=1011.2480.3001.8118
int main()
{
    //test_safty_spdlog();
    //test_sync_colorout();
    //test_sync_fileout();
    //test_async_spdlog();
}
